//
//  MTLoginPhoneVC.m
//  met
//
//  Created by 李晓俊 on 2019/5/8.
//  Copyright © 2019 李晓俊. All rights reserved.
//

#import "MTLoginPhoneVC.h"
#import "MTSetSexVC.h"
#import "MTShouQuanVC.h"
#import "MTXieYiVC.h"
#import "GYWebviewctl.h"
@interface MTLoginPhoneVC ()
@property (weak, nonatomic) IBOutlet UIButton *getRegister;
@property (weak, nonatomic) IBOutlet UITextField *phone;
@property (weak, nonatomic) IBOutlet UITextField *code;
@property (weak, nonatomic) IBOutlet UIButton *nextBut;
@property (weak, nonatomic) IBOutlet UIView *xian;
@property (weak, nonatomic) IBOutlet UILabel *xieyi;
@property (weak, nonatomic) IBOutlet UILabel *zhengce;

@end

@implementation MTLoginPhoneVC {
    UILabel *_codelabel;
    NSTimer *_timer;
    int      timeDown;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    [self wr_setNavBarBackgroundAlpha:0];
    self.nextBut.layer.borderWidth = 2;
    self.nextBut.layer.borderColor = KBlackColor.CGColor;
    self.getRegister.layer.borderWidth = 2;
    self.getRegister.layer.borderColor = KBlackColor.CGColor;
    [self.phone addTarget:self action:@selector(phoneChanged:) forControlEvents:UIControlEventEditingChanged];
    [self.code addTarget:self action:@selector(codeChanged:) forControlEvents:UIControlEventEditingChanged];

    self.nextBut.backgroundColor = HEXCOLOR(0xCDD3DB);
    self.nextBut.userInteractionEnabled = NO;
    
    self.xian.backgroundColor = KMineLine;
    
    self.xieyi.userInteractionEnabled = YES;
    UITapGestureRecognizer *xy = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(xyClick)];
    [self.xieyi addGestureRecognizer:xy];
    
    self.zhengce.userInteractionEnabled = YES;
    UITapGestureRecognizer *zc = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(zcClick)];
    [self.zhengce addGestureRecognizer:zc];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    // 设置导航栏默认的背景颜色
    [WRNavigationBar wr_setDefaultNavBarBarTintColor:KWhiteColor];
}
- (void)phoneChanged:(UITextField *)text {
    if (text.text.length >= 11) {
//        校验手机号
        BOOL isPhone = [self IsPhoneNumber:[text.text substringToIndex:11]];
        if (isPhone == YES) {
            //是手机号
            self.xian.backgroundColor = [UIColor blackColor];
            self.code.userInteractionEnabled = YES;
        }else {
            self.xian.backgroundColor = KMineLine;
            self.code.userInteractionEnabled = NO;
        }
        [self.view endEditing:YES];
    }else {
        self.xian.backgroundColor = KMineLine;
        self.code.userInteractionEnabled = NO;
    }
    
    if (text.text.length > 11) {
        text.text = [text.text substringToIndex:11];
    }
}
- (void)codeChanged:(UITextField *)text {
    if (text.text.length > 0) {
        self.nextBut.backgroundColor =  HEXCOLOR(0xFEF45C);
        self.nextBut.userInteractionEnabled = YES;
    }else {
        self.nextBut.backgroundColor = HEXCOLOR(0xCDD3DB);
        self.nextBut.userInteractionEnabled = NO;
    }
   
    if (text.text.length > 6) {
        text.text = [text.text substringToIndex:6];
    }
    if (text.text.length == 6) {
        [self.view endEditing:YES];
        
    }
}
- (BOOL)IsPhoneNumber:(NSString *)number
{
    NSString *phoneRegex1=@"1[34578]([0-9]){9}";
    NSPredicate *phoneTest1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex1];
    return  [phoneTest1 evaluateWithObject:number];
}
- (IBAction)xieyiCLick:(id)sender {
}



- (IBAction)getRes:(UIButton *)sender {
    [self.view endEditing:YES];
    if (![self IsPhoneNumber:self.phone.text]) {
//        [self.view makeToast:@"请先正确手机号"];
        [self.view maketishiToast:@"请先输入正确手机号"];
//        [self.view maketicketToast:@"1"];

        return;
    }
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    AccountService *acc = [AccountService getObject];
    acc.httpRequestResultBlock = ^(id data,int code){
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (code == 1) {
            [self.code becomeFirstResponder];
//            [self.view maketishiToast:@"获取验证码成功"];
            timeDown = 60;
            //覆盖一个uilable到button上
            _codelabel = [[UILabel alloc] initWithFrame:_getRegister.bounds];
            _codelabel.textAlignment = NSTextAlignmentCenter;
            _codelabel.font = _getRegister.titleLabel.font;
            _codelabel.textColor = TextTiTleColor;
            _codelabel.backgroundColor = [UIColor whiteColor];
            [_getRegister addSubview:_codelabel];
            //不让继续点击
            self.getRegister.userInteractionEnabled = NO;
            //开启计时器
            _timer = [NSTimer timerWithTimeInterval:1.0f target:self selector:@selector(codeAdd) userInfo:nil repeats:YES];
            [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
            [_timer fire];
        }else {
            [self.view makeToast:data];
        }
    };
    [acc sendCode:self.phone.text];
    
}
-(void)codeAdd{
    _codelabel.text = [NSString stringWithFormat:@"%ds后重新获取",timeDown--];
    if(timeDown == -1){
        [_codelabel removeFromSuperview];
        _codelabel = nil;
        [_timer invalidate];
        _timer = nil;
        self.getRegister.userInteractionEnabled = YES;
    }
}
- (IBAction)nextButClick:(id)sender {
    [self.view endEditing:YES];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    AccountService *acc = [AccountService getObject];
    acc.httpRequestResultBlock = ^(id data,int code){
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (code == 1) {
//            [self.view makeToast:@"登录成功"];
            KUSER = [UserInfo mj_objectWithKeyValues:[data objectForKey:@"user"]];

            [JPUSHService setAlias:self.phone.text completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
                
            } seq:1];
            
            
            NSUserDefaults *userInfo = [NSUserDefaults standardUserDefaults];
            NSDictionary *dic = [KUSER mj_keyValues];
            // 操作者将包含用户信息的数组arr持久化
            [userInfo setValue:dic forKey:@"userinfo"];
            [userInfo setValue:[data objectForKey:@"token"] forKey:@"token"];
            //把值立即存进去,放置数据丢失
            [[NSUserDefaults standardUserDefaults]synchronize];
            if ([[NSString stringWithFormat:@"%@",KUSER.nickName] length] == 0) {
                //没有数据
                    MTSetSexVC *vc = GetViewControllerInStoryboard(@"Login", @"MTSetSexVC");
                    [self.navigationController pushViewController:vc animated:YES];
            }else {
                //有数据
                MTShouQuanVC *vc = GetViewControllerInStoryboard(@"Login", @"MTShouQuanVC");
                [self.navigationController pushViewController:vc animated:YES];
            }
        
        }else {
            [self.view maketishiToast:data];
        }
    };
    [acc LoginPhone:self.phone.text smsCode:self.code.text];
//LoginPhone:self.phone.text loginPwd:self.pwd.text];
}


-(void)xyClick {
    //服务协议
//    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    AccountService *acc = [AccountService getObject];
    acc.httpRequestResultBlock = ^(id data,int code){
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (code == 1) {
            if ([[NSString stringWithFormat:@"%@",[data objectForKey:@"model"]]isEqualToString:@"1"]) {
                MTXieYiVC *vc = GetViewControllerInStoryboard(@"Mine", @"MTXieYiVC");
                vc.name= @"服务协议";
                vc.content = [NSString stringWithFormat:@"%@",[data objectForKey:@"content"]];
                [self.navigationController pushViewController:vc animated:YES];
            }else {
                
                GYWebviewctl *clt = [[GYWebviewctl alloc] init];
                clt.gyweb = GYWebviewctlFuwu;
                clt.jumpUrl = [NSString stringWithFormat:@"%@",[data objectForKey:@"content"]];
                [self.navigationController pushViewController:clt animated:YES];
            }
            
        }else {
            [self.view maketishiToast:data];
        }
    };
    [acc findOthers:@"1"];
}
-(void)zcClick {
    //隐私条款
//    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    AccountService *acc = [AccountService getObject];
    acc.httpRequestResultBlock = ^(id data,int code){
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (code == 1) {
            if ([[NSString stringWithFormat:@"%@",[data objectForKey:@"model"]]isEqualToString:@"1"]) {
                MTXieYiVC *vc = GetViewControllerInStoryboard(@"Mine", @"MTXieYiVC");
                vc.name= @"隐私条款";
                vc.content = [NSString stringWithFormat:@"%@",[data objectForKey:@"content"]];
                [self.navigationController pushViewController:vc animated:YES];
            }else {
                
                GYWebviewctl *clt = [[GYWebviewctl alloc] init];
                clt.gyweb = GYWebviewctlUsr;
                clt.jumpUrl = [NSString stringWithFormat:@"%@",[data objectForKey:@"content"]];
                [self.navigationController pushViewController:clt animated:YES];
            }
        }else {
            [self.view maketishiToast:data];
        }
    };
    [acc findOthers:@"2"];
}
@end
